package a5.grammar.productions;

import java.util.LinkedList;
import java.util.List;

import a5.Interpreter;
import a5.common.ProgramCursor;
import a5.common.OrderedTree;
import a5.common.Token;
import a5.environment.Environment;
import a5.grammar.annotations.Grammar;
import a5.grammar.grammars.G1;
import a5.grammar.interfaces.Production;

/**
 * <pre>
 * This class provides a method for interpreting the following production
 * Expr -> IntegerLiteral | VariableName | Application
 * 
 * In plain text, the end result of this production will be a single value, be it an IntegerLiteral,
 * a VariableName, or the result of {@link a5.grammar.productions.Application#interpret(Interpreter) Application.interpret()}.
 * 
 * This production uses recursive decent to interpret and parse.
 * </pre>
 * 
 * @author Jason Campos
 * @version Assignment 5, CS 152 (Programming Paradigms) Fall 2012, Jeffrey Smith
 */
@Grammar(clazz=G1.class)
public class Expr implements Production{
	// Production dependency
	private Application application;
	
	// Hide constructor
	public Expr(){}
	
	/**
	 * Interprets the program being examined by the argument interpreter using the defined rules for this production. 
	 * 
	 * @param environment
	 * The {@link a5.environment.Environment Environment} responsible for variable and function maintenance
	 * and scoping. 
	 * 
	 * @param cursor
	 * The {@link a5.common.ProgramCursor ProgramCursor} which contains the {@link a5.common.Token Tokens} 
	 * of the program being interpreted.
	 * 
	 * @return
	 * In the case of an "IntegerLiteral" or "VariableName", this method will simply return
	 * the text value. If the current point of execution does not point to one of the 
	 * aforementioned types, then this method will return the result of 
	 * {@link a5.grammar.productions.Application#interpret(Interpreter) Application.interpret()}.
	 */
	public String interpret(Environment environment, ProgramCursor cursor) {
		if(cursor.first("IntegerLiteral"))
			return cursor.match(environment, "IntegerLiteral");
		if(cursor.first("VariableName"))
			return environment.getVariable(cursor.match(environment, "VariableName"));
		
		return application.interpret(environment, cursor);
	}
	
	/**
	 * Recursively parses the program of the argument cursor using the variable/function scoping
	 * policies of the argument environment.
	 * 
	 * @param cursor
	 * The {@link a5.common.ProgramCursor ProgramCursor} which contains the {@link a5.common.Token Tokens} 
	 * of the program being interpreted.
	 * 
	 * @return
	 * An {@link a5.common.OrderedTree OrderedTree<Token>} representing the parse structure of the 
	 * the program of the argument cursor.
	 */
	public OrderedTree<Token> parse(ProgramCursor cursor){
		List<OrderedTree<Token>> children = new LinkedList<OrderedTree<Token>>();
		
		// Peek at the token type to determine which element to add to the parse tree
		if(cursor.first("IntegerLiteral")) 
			children.add(new OrderedTree<Token>(new Token(cursor.match("IntegerLiteral"))));
		else if (cursor.first("VariableName"))
			children.add(new OrderedTree<Token>(new Token(cursor.match("VariableName"))));
		else 
			children.add(application.parse(cursor));
		
		return new OrderedTree<Token>(new Token(this.getClass().getSimpleName()), children);
	}
	
	/**
	 * @param cursor
	 * The {@link a5.common.ProgramCursor ProgramCursor} which contains the {@link a5.common.Token Tokens} 
	 * of the program being interpreted.
	 * 
	 * @return
	 * {@code true} if the execution point of the program is at a point which indicates that
	 * an Expr is a valid path of execution. In other words, if the current point of
	 * execution contains an element which is the first element of this production 
	 * ("VariableName", "IntegerLiteral", Application).
	 */
	public boolean first(ProgramCursor cursor) {
		return cursor.first("VariableName") || cursor.first("IntegerLiteral") || application.first(cursor);
	}
}
